


use "$data/DAILY.PANEL.dta", clear

keep if date<=21977

merge m:1 iso2c year month day using "$data/COVID.DAILY.dta"
keep if _merge==1 | _merge==3
drop _merge

merge m:1 iso2c using "$data/first_hth_transmission_NOMISSING.dta"
keep if _merge==1 | _merge==3
drop _merge

replace date_first_hth = 0 if date_first_hth==.

gen postfirsthth = date>=date_first_hth 


merge m:1 iso2c year month day using "$data/GOOGLE.TRENDS.DAILY.dta"
keep if _merge==1 | _merge==3
drop _merge



capture label drop google
label define google 1 "recession_hits" 2  "stockmarket_crash_hits" 3 "conspiracy_theory_hits" 4   "prepper_hits"
global googlesearchvar = "recession_hits  stockmarket_crash_hits conspiracy_theory_hits  prepper_hits"



encode iso2c , gen(iso)
sort iso date


xtset iso date

format date %td


destring $googlesearchvar, replace force

sort iso date

foreach var in covid_confirmed covid_death covid_recovered {

replace `var' = 0 if  `var'==. 

}


foreach var in covid_confirmed covid_death covid_recovered {

gen any`var' = `var'>0 if `var'!=. 

}

label variable anycovid_confirmed "First confirmed COVID19 case"
label variable anycovid_death "First confirmed COVID19 death"


gen temp = date if anycovid_confirmed ==1
egen mintemp = min(temp) , by(iso )
egen firstcorona = min(temp) , by(iso )
gen timetofirst = date-firstcorona 
drop temp

replace firstcorona = 51977 if firstcorona ==.

foreach var in $googlesearchvar  {

egen temp = mean(`var') if date<firstcorona, by(iso2c)

egen pre`var' = mean(temp), by(iso2c)
drop temp

}

foreach var in $googlesearchvar {

replace `var' = `var'/(pre`var'+1)

}



keep if year==2020
eststo clear
 
 
loc experiments "recession_hits  stockmarket_crash_hits conspiracy_theory_hits  prepper_hits"
		   
			   


****LEAVE ONE OUT

foreach depvar in  $googlesearchvar  {

preserve
keep if year==2020
reghdfe  `depvar'   anycovid_confirmed      , absorb(iso date) vce(cl iso )
lincom anycovid_confirmed
global avgeffect = `r(estimate)'

tab iso2c if e(sample), gen(cc_)

encode iso2c, gen(c)

local states = `r(r)'
matrix hrid = J(`states',7,.)

estimates clear
forvalues i=1(1)`states' { 

qui reghdfe  `depvar'   anycovid_confirmed  if cc_`i'!=1    , absorb(iso date) vce(cl iso )

su c if cc_`i'==1
  matrix hrid[`i',1] = `r(mean)'
lincom anycovid_confirmed
  matrix hrid[`i',2] = `r(estimate)'
  matrix hrid[`i',3] = `r(se)'
  matrix hrid[`i',4] = `r(estimate)'/`r(se)'

}

svmat hrid

label values hrid1 c

label variable hrid2 "Distribution of estimates"
su hrid2, det

keep hrid*
drop if hrid1==. & hrid2==. & hrid3==.

replace hrid5 = $avgeffect
rename hrid1 countrydropped
rename hrid2 estimate
rename hrid3 se
rename hrid4 tstat




outsheet using "$figures/raw/leave-on-out-country-`depvar'.csv", delimiter(,) replace

restore
}




foreach depvar in  $googlesearchvar  {

preserve
keep if year==2020
reghdfe  `depvar'   anycovid_confirmed      , absorb(iso date) vce(cl iso )
lincom anycovid_confirmed
global avgeffect = `r(estimate)'

tab sub_region if e(sample), gen(sr_)

encode sub_region, gen(sr)

local states = `r(r)'
matrix hrid = J(`states',7,.)

estimates clear
forvalues i=1(1)`states' { 

qui reghdfe  `depvar'   anycovid_confirmed  if sr_`i'!=1    , absorb(iso date) vce(cl iso )

su sr if sr_`i'==1
  matrix hrid[`i',1] = `r(mean)'
lincom anycovid_confirmed
  matrix hrid[`i',2] = `r(estimate)'
  matrix hrid[`i',3] = `r(se)'
  matrix hrid[`i',4] = `r(estimate)'/`r(se)'

}

svmat hrid

label values hrid1 sr

label variable hrid2 "Distribution of estimates"
su hrid2, det

keep hrid*
drop if hrid1==. & hrid2==. & hrid3==.

replace hrid5 = $avgeffect
rename hrid1 countrydropped
rename hrid2 estimate
rename hrid3 se
rename hrid4 tstat


outsheet using "$figures/raw/leave-on-out-region-`depvar'.csv", delimiter(,) replace

restore
}
